﻿using MQTTnet;
using MQTTnet.Server;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Juli.Mes.Mqtt.Extensions
{
    public static class MqttServerExtensions
    {
        public static void PublishByHub(this MqttServer mqttServer, string topic, string payload)
        {
            if (topic == null) throw new ArgumentNullException(nameof(topic));

            mqttServer.PublishByHub(new MqttApplicationMessageBuilder()
                .WithTopic(topic)
                .WithPayload(payload)
                .Build());
        }

        public static void PublishByHub(this MqttServer mqttServer, MqttApplicationMessage mqttApplicationMessage)
        {
            if (mqttServer == null) throw new ArgumentNullException(nameof(mqttServer));
            if (mqttApplicationMessage == null) throw new ArgumentNullException(nameof(mqttApplicationMessage));

            mqttServer.InjectApplicationMessage(new InjectedMqttApplicationMessage(mqttApplicationMessage)
            {
                SenderClientId = MqttServiceConsts.IotCloudMqttClientId
            });

        }


    }
}
